package com.lz.controller.ly;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lz.entity.AuditLogging;
import com.lz.entity.Employee;
import com.lz.entity.EquipmentSheet;
import com.lz.entity.InstallationPerson;
import com.lz.entity.UpholsteryType;
import com.lz.service.ly.EquipmentSheetByStateService;
import com.lz.vo.ly.EquipmentSheetConditionVO;

import util.BackUtil;
import util.PageUitl;

@Controller
public class EquipmentSheetByStareController {

	@Autowired
	private EquipmentSheetByStateService stateService;
	
	@RequestMapping("/getEquipmentSheetByState")
	public String getEquipmentSheet(Model model,Integer pageNo,EquipmentSheet sheet,String start_time,String end_time) {
		
		Integer rows = stateService.getEsByState(sheet,start_time,end_time);
		PageUitl<EquipmentSheetConditionVO> pageInfo = new PageUitl<>(pageNo, 2, rows);
		List<EquipmentSheetConditionVO> lists = stateService.getEquipmentSheetByState((pageInfo.getPageNo()-1)*2,2,sheet,start_time,end_time);
		pageInfo.setList(lists);
		model.addAttribute("pageInfo", pageInfo);
		//获取所有装潢业务类型
		List<UpholsteryType> listType = stateService.getType();
		model.addAttribute("listType", listType);
		//把参数带过去
		model.addAttribute("map", BackUtil.back(sheet,start_time,end_time));
		
		//获取接单人
		List<InstallationPerson> personList = stateService.getInstallationPersonList();
		model.addAttribute("personList", personList);
		
		return "afterSale";
	}
	
	@RequestMapping(value="/updEquipmentSheetState",produces="application/json;charset=utf-8")
	@ResponseBody
	public String updEquipmentSheetState(String result,Integer equipmentSheetNo,String auditOpinion) {
		if(result != null && equipmentSheetNo != null) {
			//根据id获取装潢单信息
			EquipmentSheet sheet = stateService.getEquipmentSheetById(equipmentSheetNo);
			
			//获取接单人信息
			AuditLogging logging = new AuditLogging();
			logging.setEquipmentSheetNo(sheet.getEquipmentSheetNo());
			logging.setOpeningTime(sheet.getOpeningTime());
			logging.setAuditTime(new Date());
			logging.setAuditResult(result);
			logging.setAuditName("考拉");
			logging.setAuditType("经理审核");
			logging.setAuditOpinion(auditOpinion);
			//保存审核记录
			stateService.savaAuditLogging(logging);
			boolean s = stateService.updEquipmentSheetState(result,equipmentSheetNo);
			return JSON.toJSONString(s);
		}
		return null;
	}
	
	@RequestMapping(value="/authorityManagement",produces="application/json;charset=utf-8")
	@ResponseBody
	public String authorityManagement(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Employee employee = (Employee) session.getAttribute("employee");
		if(employee != null) {
			return JSON.toJSONString(employee);
		}
		return null;
	}
	
}
